/* This is the game class. It contains the whole status of the game and you save and load in this class.
 * This class will read and write a player to a .sav file and the map to a .map file in the save and load methods respectively
 * This class will also run the main part of the game
 */

package game;

import item.*;

import java.io.*;
import java.util.*;

import map.*;
import map.Map;

public class Game {

	private Player player; //the player object that holds all of the player information
	private Map map; //the map object holding the status of the map
	
	
	//0 param constructor setting everything to defaults
	public Game(){
		
		player = null;
		map = null;
	}//default constructor
	
	
	//Sets up a new game
	public void newGame(){
		
		Scanner in = new Scanner(System.in); //used to read information from the user;
		String name = null; //the name for the new Game
		int difficulty; //the difficulty the player chooses
		
		
		System.out.print("Enter player name: ");
		name = in.nextLine();
		
		System.out.print("\n\nChoose a difficulty.\n" +
				"1. Very Easy\n" +
				"2. Easy\n" +
				"3. Normal\n" +
				"4. Hard\n" +
				"5. Brutal\n" +
				"Choice: ");
		
		difficulty = in.nextInt();
		
		if(difficulty == 3)
			player = new Player(name);
		
		else{
			
			switch(difficulty){
			
			case 1:
				player = new Player(name, Difficulty.VERY_EASY);
				break;
				
			case 2:
				player = new Player(name, Difficulty.EASY);
				break;
				
			case 4:
				player = new Player(name, Difficulty.HARD);
				break;
				
			default:
				player = new Player(name, Difficulty.BRUTAL);
			}//switch difficulty		
		}//else
			
	}//newGame
	
	
	//Saves the game to a file named [playername].sav and [playername].map which are placed in the "Space Game"
	//folder in the project folder
	public void save(){
		
		try{
			
			FileOutputStream fP = new FileOutputStream("SpaceGame\\" + player.getName() + ".sav"); //for writing the player file
			FileOutputStream fM = new FileOutputStream("SpaceGame\\" + player.getName() + ".map"); //for writing the map file
			ObjectOutputStream oP = new ObjectOutputStream(fP); //for writing the player file
			ObjectOutputStream oM = new ObjectOutputStream(fM); //for writing the map file
			
			oP.writeObject(player);
			oM.writeObject(map);
			
			//closes all the streams
			fP.close();
			fM.close();
			oP.close();
			oM.close();				
		}catch(IOException e){
			
			System.out.println("@1 Error at saving.");
			e.printStackTrace();
		}//try catch
	}//save
	

	//Loads saves. Opens a menu from availible save files
	public void load(){
		
		try {
			
			Scanner in = new Scanner(System.in); //for reading in the user input for loading
			int choice; //the save they choose to load
			File f = new File("SpaceGame\\"); //for finding the saves able to be loaded
			String[] files = f.list();
			
			for(int i = 0; i < files.length; i += 2){
				
				System.out.println(((i / 2) + 1) + ". " + files[i].substring(0, files[i].indexOf('.')));
			}//for
			
			System.out.print("\nPlease choose save to load.");
			choice = (in.nextInt() - 1) * 2;

			FileInputStream fP = new FileInputStream("SpaceGame\\" + files[choice + 1]); //used for reading the player
			FileInputStream fM = new FileInputStream("SpaceGame\\" + files[choice]); //used for reading the map
			ObjectInputStream oP = new ObjectInputStream(fP); //used for reading the player
			ObjectInputStream oM = new ObjectInputStream(fM); //used for reading the map

			player = (Player) oP.readObject();
			map = (Map) oM.readObject();
			
			//closes all the streams
			in.close();
			oP.close();
			oM.close();
			fP.close();
			fM.close();
		} catch (Exception e) {

			System.out.println("@2 Error at loading.");
			e.printStackTrace();
		}//try catch
	}//load
}//Game